package encaps.action;

import encaps.entities.Grant;
import encaps.entities.Person;
import encaps.entities.PersonLocal;
import encaps.entities.PersonLocalHome;
import encaps.logging.JMSUtil;
import encaps.logging.Log;
import encaps.resources.Config;
import encaps.resources.Image;
import encaps.resources.Labels;
import encaps.resources.Response;
import encaps.utils.*;
import java.util.Date;
import javax.ejb.FinderException;
import javax.naming.NamingException;
import org.jdom.Element;

public class ApplyPersonChangesAction extends ShowEmptyPersonPageAction {

    public static final String APPLY_PERSON_CHANGES = "applyPersonChanges";
    private PersonLocal person;
    private Long id;

    public ApplyPersonChangesAction(ActionContext context, Grant requiredGrant) {
        super(context, requiredGrant);
    }

    @Override
    public Element execute() throws ActionExecutionException {
        try {
            id = Long.valueOf(actionContext.getTextParameter(ID));

            PersonLocalHome personHome = EntityUtil.getPersonHome();
            person = personHome.findByPrimaryKey(id);

            Element dataToReenter = validate(person);
            if (dataToReenter != null) {
                return dataToReenter;
            }

            String name = actionContext.getTextParameter(PERSON_NAME);
            String bio = actionContext.getTextParameter(PERSON_BIOGRAPHY);

            String picName = String.format("%d", person.getId());
            String picPath = IOHelper.uploadFile(actionContext, PERSON_PIC, picName, Config.getPersonImagesDirectory());

            person.setBio(bio);
            person.setName(name);
            if (picPath != null) {
                person.setPhoto(picPath);
            }

            JMSUtil.sendLogMessage(Log.UPDATE,
                    EntityUtil.getUserHome().findByLogin(actionContext.getUserLogin()).getID(),
                    actionContext.getUserLogin(), new Date(), Person.TABLE_NAME,
                    person.getId(), person.getName());

            return XMLHelper.createTextMessage(Labels.getLabel(Labels.PERSON_CHANGED_MESSAGE));
        } catch (SystemException ex) {
            throw new ActionExecutionException(Labels.getLabel(Labels.CANNOT_EXECUTE_ACTION), ex);
        } catch (FinderException ex) {
            throw new ActionExecutionException(Labels.getLabel(Labels.CANNOT_EXECUTE_ACTION), ex);
        } catch (NamingException ex) {
            throw new ActionExecutionException(Labels.getLabel(Labels.CANNOT_FIND_LOCAL_HOME_OBJECT_MESSAGE), ex);
        }
    }

    private Element validate(PersonLocal person) throws SystemException {
        String name = actionContext.getTextParameter(PERSON_NAME);
        if (!StringUtil.isEmpty(name)) {
            return null;
        }
        Element result = XMLHelper.createElement(Response.RESPONSE);
        Element e = XMLHelper.addChild(result, Response.ENTITY);
        e.setAttribute(Response.EDIT, "true");

        XMLHelper.addChild(e, XMLHelper.createHeader(Labels.getLabel(Labels.CREATE_PERSON)));
        XMLHelper.addChild(e, XMLHelper.createImage(person.getPhoto(), PERSON_PIC));
        XMLHelper.addChild(e, XMLHelper.createTextField(Labels.getLabel(Labels.PERSON_NAME), name, PERSON_NAME, Labels.getLabel(Labels.EMPTY_NAME_FIELD_MESSAGE)));
        XMLHelper.addChild(e, XMLHelper.createTextArea(Labels.getLabel(Labels.PERSON_BIOGRAPHY), actionContext.getTextParameter(PERSON_BIOGRAPHY), PERSON_BIOGRAPHY));
        XMLHelper.addChild(e, XMLHelper.createHiddenTextField(id, ID));

        XMLHelper.addChild(e, XMLHelper.createImageButton(Image.getImage(Image.APPLY), APPLY_PERSON_CHANGES));
        return result;
    }
}